package com.haixee.parkdemo.auth.controller;

import com.haixee.parkdemo.auth.entity.request.GroupRequest;
import com.haixee.parkdemo.auth.service.GroupService;
import com.haixee.parkdemo.auth.service.UserService;
import com.haixee.parkdemo.common.response.ResResult;
import com.haixee.parkdemo.common.response.ResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public ResResult add(@RequestBody GroupRequest request) {
       groupService.addGroup(request.getName(), request.getParentId(), request.getRoleIdList());
       return ResUtil.success();
    }

    @PostMapping("/add/user")
    public ResResult addUser(@RequestBody GroupRequest request) {
        groupService.addUser(request.getParentId(), request.getUid());
        return ResUtil.success();
    }

    @PostMapping("/add/organize")
    public ResResult addOrganize(@RequestBody GroupRequest request) {
        groupService.add(request.getGroupId(), request.getOrganizeId());
        return ResUtil.success();
    }

//    @GetMapping("/all")
//    public ResResult getAll() {
//        List<Group> groupList = groupService.getAll();
//
//        List<User> userList = userService.getAllUserJob();
//
//        List<GroupOrgDTO> orgList = groupService.getAllGroupOrganize();
//
//        List<OrganizeTreeVO> unmountList = new ArrayList<>();
//
//        Map<String, OrganizeTreeVO> map = new HashMap<>();
//        OrganizeTreeVO theVO = new OrganizeTreeVO();
//        map.put("group0", theVO);
//
//
//        for (Group organize : groupList) {
//            OrganizeTreeVO vo = new OrganizeTreeVO();
//
//            vo.setId("group" + organize.getId());
//            vo.setGroupId(organize.getId());
//            vo.setType((byte)1);
//
//            vo.setParentId(organize.getParentId());
//            vo.setName(organize.getName());
//            map.put(vo.getId(), vo);
//
//            OrganizeTreeVO parentVO = map.get("group" + organize.getParentId());
//            if (parentVO != null) {
//                List<OrganizeTreeVO> children = parentVO.getChildren();
//                children.add(vo);
//            } else {
//                unmountList.add(vo);
//            }
//        }
//
//        for (OrganizeTreeVO organizeTreeVO : unmountList) {
//            OrganizeTreeVO parentVO = map.get("group" + organizeTreeVO.getParentId());
//            parentVO.getChildren().add(parentVO);
//        }
//
//        for (User user : userList) {
//            OrganizeTreeVO vo = new OrganizeTreeVO();
//            vo.setId("uid" + user.getId());
//            vo.setUserId(user.getId());
//            vo.setParentId(user.getJobId());
//            vo.setType((byte) 3);
//            vo.setName(user.getUsername());
//            OrganizeTreeVO parentVO = map.get("group" + user.getJobId());
//            if (parentVO != null) {
//                parentVO.getChildren().add(vo);
//            }
//
//        }
//
//        for (GroupOrgDTO groupOrgDto : orgList) {
//            OrganizeTreeVO vo = new OrganizeTreeVO();
//            vo.setId("org" + groupOrgDto.getOrganizeId());
//            vo.setOrganizeId(groupOrgDto.getOrganizeId());
//            vo.setParentId(groupOrgDto.getGroupId());
//            vo.setType((byte) 2);
//            vo.setName(groupOrgDto.getName());
//            OrganizeTreeVO parentVO = map.get("group" + groupOrgDto.getGroupId());
//            if (parentVO != null) {
//                parentVO.getChildren().add(vo);
//            }
//        }
//
//        return ResUtil.success(theVO.getChildren());
//    }


}
